{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ccb2c9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "a1237912",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.abc import x,y,z,t,theta,phi,psi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0d6ce71",
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b=symbols(\"a,b\",positive=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c2ab9de0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\phi$"
      ],
      "text/plain": [
       "phi"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41b5dc5d",
   "metadata": {},
   "source": [
    "## 斜二测画法画一般性的立体图形"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b8c539b",
   "metadata": {},
   "source": [
    "首先，我们可以确定斜二测画法的旋转矩阵："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "553d04d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & t \\cos{\\left(\\phi \\right)} & 0\\\\0 & t \\sin{\\left(\\phi \\right)} & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, t*cos(phi), 0],\n",
       "[0, t*sin(phi), 1]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trans=Matrix([[1,t*cos(phi),0],[0,t*sin(phi),1]])\n",
    "trans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e3930259",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}t y \\cos{\\left(\\phi \\right)} + x\\\\t y \\sin{\\left(\\phi \\right)} + z\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[t*y*cos(phi) + x],\n",
       "[t*y*sin(phi) + z]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trans*Matrix([x,y,z])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b7abc0e",
   "metadata": {},
   "source": [
    "### 圆的斜二测画法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d4c40067",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.abc import r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a6cb0c9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "circle_theta=Matrix([r*cos(theta),r*sin(theta),0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "072864c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}r t \\sin{\\left(\\theta \\right)} \\cos{\\left(\\phi \\right)} + r \\cos{\\left(\\theta \\right)}\\\\r t \\sin{\\left(\\phi \\right)} \\sin{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[r*t*sin(theta)*cos(phi) + r*cos(theta)],\n",
       "[               r*t*sin(phi)*sin(theta)]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trans*circle_theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "aecde177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}r \\left(t \\sin{\\left(\\theta \\right)} \\cos{\\left(\\phi \\right)} + \\cos{\\left(\\theta \\right)}\\right)\\\\r t \\sin{\\left(\\phi \\right)} \\sin{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[r*(t*sin(theta)*cos(phi) + cos(theta))],\n",
       "[               r*t*sin(phi)*sin(theta)]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trigsimp(trans*circle_theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "54076c5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_circle_quare=(trans*circle_theta)[0]**2+(trans*circle_theta)[1]**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cd368926",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle r^{2} t^{2} \\sin^{2}{\\left(\\phi \\right)} \\sin^{2}{\\left(\\theta \\right)} + r^{2} t^{2} \\sin^{2}{\\left(\\theta \\right)} \\cos^{2}{\\left(\\phi \\right)} + 2 r^{2} t \\sin{\\left(\\theta \\right)} \\cos{\\left(\\phi \\right)} \\cos{\\left(\\theta \\right)} + r^{2} \\cos^{2}{\\left(\\theta \\right)}$"
      ],
      "text/plain": [
       "r**2*t**2*sin(phi)**2*sin(theta)**2 + r**2*t**2*sin(theta)**2*cos(phi)**2 + 2*r**2*t*sin(theta)*cos(phi)*cos(theta) + r**2*cos(theta)**2"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(expand(trans_circle_quare))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "f7204168",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{r^{2} \\left(- t^{2} \\cos{\\left(2 \\theta \\right)} + t^{2} - t \\sin{\\left(\\phi - 2 \\theta \\right)} + t \\sin{\\left(\\phi + 2 \\theta \\right)} + \\cos{\\left(2 \\theta \\right)} + 1\\right)}{2}$"
      ],
      "text/plain": [
       "r**2*(-t**2*cos(2*theta) + t**2 - t*sin(phi - 2*theta) + t*sin(phi + 2*theta) + cos(2*theta) + 1)/2"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(expand(trans_circle_quare))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30118862",
   "metadata": {},
   "source": [
    "### 将上式进行化简"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9c3ea9e6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{r^{2} \\left(t^{2} + 2 t \\sin{\\left(2 \\theta \\right)} \\cos{\\left(\\phi \\right)} + \\left(1 - t^{2}\\right) \\cos{\\left(2 \\theta \\right)} + 1\\right)}{2}$"
      ],
      "text/plain": [
       "r**2*(t**2 + 2*t*sin(2*theta)*cos(phi) + (1 - t**2)*cos(2*theta) + 1)/2"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr=(r**2/2)*((1-t**2)*cos(2*theta)+(2*t*cos(phi)*sin(2*theta)+(t**2+1)))\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b712e016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(expr-trans_circle_quare)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33a01595",
   "metadata": {},
   "source": [
    "#### 我们将化简后的式子表示成三角函数的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d16d82b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{atan}{\\left(\\frac{2 t \\cos{\\left(\\phi \\right)}}{1 - t^{2}} \\right)}$"
      ],
      "text/plain": [
       "atan(2*t*cos(phi)/(1 - t**2))"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "psi=atan((2*t*cos(phi))/(1-t**2))\n",
    "psi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "6869d335",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{4 t^{2} \\cos^{2}{\\left(\\phi \\right)} + \\left(1 - t^{2}\\right)^{2}}$"
      ],
      "text/plain": [
       "sqrt(4*t**2*cos(phi)**2 + (1 - t**2)**2)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=sqrt((2*t*cos(phi))**2+(1-t**2)**2)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc9959dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "expr2=d*cos(2*(theta-psi/2))+"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba579fcf",
   "metadata": {},
   "source": [
    "## 定义几个基本的几何变换矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b970f768",
   "metadata": {},
   "source": [
    "### 平面旋转矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d949555c",
   "metadata": {},
   "outputs": [],
   "source": [
    "point_xy=Matrix([x,y])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2748ebec",
   "metadata": {},
   "source": [
    "旋转参数为$\\theta$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0400f227",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\cos{\\left(\\theta \\right)} & - \\sin{\\left(\\theta \\right)}\\\\\\sin{\\left(\\theta \\right)} & \\cos{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[cos(theta), -sin(theta)],\n",
       "[sin(theta),  cos(theta)]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rot=Matrix([[cos(theta),-sin(theta)],[sin(theta),cos(theta)]])\n",
    "rot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1c155b93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0\\\\0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 0],\n",
       "[0, 1]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(rot.inv()*rot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "45fdc839",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x \\cos{\\left(\\theta \\right)} - y \\sin{\\left(\\theta \\right)}\\\\x \\sin{\\left(\\theta \\right)} + y \\cos{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[x*cos(theta) - y*sin(theta)],\n",
       "[x*sin(theta) + y*cos(theta)]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rot*point_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "064f991f",
   "metadata": {},
   "source": [
    "### 单位向量拉伸矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a866605c",
   "metadata": {},
   "source": [
    "拉伸参数为$a,b$。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "53ab5194",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & 0\\\\0 & b\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[a, 0],\n",
       "[0, b]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scope=Matrix([[a,0],[0,b]])\n",
    "scope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ab12544f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0\\\\0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 0],\n",
       "[0, 1]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(scope.inv()*scope)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4cc92b20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a x\\\\b y\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[a*x],\n",
       "[b*y]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scope*point_xy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1bf37521",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a x \\cos{\\left(\\theta \\right)} - b y \\sin{\\left(\\theta \\right)}\\\\a x \\sin{\\left(\\theta \\right)} + b y \\cos{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[a*x*cos(theta) - b*y*sin(theta)],\n",
       "[a*x*sin(theta) + b*y*cos(theta)]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rot*scope*point_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b06c451",
   "metadata": {},
   "source": [
    "#### 证明平面圆的斜二测变换可以看成是先伸缩后旋转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d85901a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "circle_xy=Matrix([x,y,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c1b1f2c",
   "metadata": {},
   "source": [
    "### 代换推导"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "475f7756",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.abc import w,u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "088a823f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}w\\\\u\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[w],\n",
       "[u]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "point_wu=Matrix([w,u])\n",
    "point_wu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2df5bf41",
   "metadata": {},
   "outputs": [],
   "source": [
    "xy_wu=simplify(scope.inv()*rot.inv()*point_wu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c06de2af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{\\frac{u \\sin{\\left(2 \\theta \\right)}}{2} + w \\cos^{2}{\\left(\\theta \\right)}}{a \\cos{\\left(\\theta \\right)}}\\\\\\frac{u \\cos{\\left(\\theta \\right)} - w \\sin{\\left(\\theta \\right)}}{b}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[(u*sin(2*theta)/2 + w*cos(theta)**2)/(a*cos(theta))],\n",
       "[                    (u*cos(theta) - w*sin(theta))/b]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(xy_wu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5ff331eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "eq2=Eq((xy_wu)[0]**2+(xy_wu)[1]**2,r**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ad294721",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{u^{2} \\cos^{2}{\\left(\\theta \\right)}}{b^{2}} - \\frac{2 u w \\sin{\\left(\\theta \\right)} \\cos{\\left(\\theta \\right)}}{b^{2}} + \\frac{w^{2} \\sin^{2}{\\left(\\theta \\right)}}{b^{2}} + \\frac{u^{2} \\sin^{2}{\\left(2 \\theta \\right)}}{4 a^{2} \\cos^{2}{\\left(\\theta \\right)}} + \\frac{u w \\sin{\\left(2 \\theta \\right)}}{a^{2}} + \\frac{w^{2} \\cos^{2}{\\left(\\theta \\right)}}{a^{2}} = r^{2}$"
      ],
      "text/plain": [
       "Eq(u**2*cos(theta)**2/b**2 - 2*u*w*sin(theta)*cos(theta)/b**2 + w**2*sin(theta)**2/b**2 + u**2*sin(2*theta)**2/(4*a**2*cos(theta)**2) + u*w*sin(2*theta)/a**2 + w**2*cos(theta)**2/a**2, r**2)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(expand(eq2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fb2f22d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle r^{2} = \\frac{a^{2} u^{2} \\cos{\\left(2 \\theta \\right)} + a^{2} u^{2} - 2 a^{2} u w \\sin{\\left(2 \\theta \\right)} - a^{2} w^{2} \\cos{\\left(2 \\theta \\right)} + a^{2} w^{2} - b^{2} u^{2} \\cos{\\left(2 \\theta \\right)} + b^{2} u^{2} + 2 b^{2} u w \\sin{\\left(2 \\theta \\right)} + b^{2} w^{2} \\cos{\\left(2 \\theta \\right)} + b^{2} w^{2}}{2 a^{2} b^{2}}$"
      ],
      "text/plain": [
       "Eq(r**2, (a**2*u**2*cos(2*theta) + a**2*u**2 - 2*a**2*u*w*sin(2*theta) - a**2*w**2*cos(2*theta) + a**2*w**2 - b**2*u**2*cos(2*theta) + b**2*u**2 + 2*b**2*u*w*sin(2*theta) + b**2*w**2*cos(2*theta) + b**2*w**2)/(2*a**2*b**2))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(expand(eq2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae17a5fc",
   "metadata": {},
   "source": [
    "又斜二测变换为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "77cc57ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}t y \\cos{\\left(\\phi \\right)} + x\\\\t y \\sin{\\left(\\phi \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[t*y*cos(phi) + x],\n",
       "[    t*y*sin(phi)]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trans*circle_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ea690a5",
   "metadata": {},
   "source": [
    "则有："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "357028af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{u}{t \\sin{\\left(\\phi \\right)}} = y$"
      ],
      "text/plain": [
       "Eq(u/(t*sin(phi)), y)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(u/(t*sin(phi)),y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "014102c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{u}{\\tan{\\left(\\phi \\right)}} + w = x$"
      ],
      "text/plain": [
       "Eq(-u/tan(phi) + w, x)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(w-u/tan(phi),x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4053dcd8",
   "metadata": {},
   "source": [
    "则有："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "59b05f3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left(- \\frac{u}{\\tan{\\left(\\phi \\right)}} + w\\right)^{2} + \\frac{u^{2}}{t^{2} \\sin^{2}{\\left(\\phi \\right)}} = r^{2}$"
      ],
      "text/plain": [
       "Eq((-u/tan(phi) + w)**2 + u**2/(t**2*sin(phi)**2), r**2)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eq1=Eq((u/(t*sin(phi)))**2+(w-u/tan(phi))**2,r**2)\n",
    "eq1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "34b0cde4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle r^{2} = \\frac{u^{2}}{\\tan^{2}{\\left(\\phi \\right)}} - \\frac{2 u w}{\\tan{\\left(\\phi \\right)}} + w^{2} + \\frac{u^{2}}{t^{2} \\sin^{2}{\\left(\\phi \\right)}}$"
      ],
      "text/plain": [
       "Eq(r**2, u**2/tan(phi)**2 - 2*u*w/tan(phi) + w**2 + u**2/(t**2*sin(phi)**2))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(eq1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bbb4284",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9b45dcab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle t^{2} = \\frac{b^{2} \\cdot \\left(1 + \\frac{1}{\\tan^{2}{\\left(\\phi \\right)}}\\right) \\tan{\\left(\\phi \\right)}}{\\tan{\\left(\\phi \\right)} + \\tan{\\left(\\theta \\right)}}$"
      ],
      "text/plain": [
       "Eq(t**2, b**2*(1 + tan(phi)**(-2))*tan(phi)/(tan(phi) + tan(theta)))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(t**2,b**2*tan(phi)/(tan(phi)+tan(theta))*(1+(1/tan(phi))**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "71ba265f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan{\\left(\\theta \\right)} = \\frac{b^{2}}{\\left(1 - b^{2}\\right) \\tan{\\left(\\phi \\right)}}$"
      ],
      "text/plain": [
       "Eq(tan(theta), b**2/((1 - b**2)*tan(phi)))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(theta),b**2/(tan(phi)*(1-b**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "038e2be3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan{\\left(\\phi \\right)} = \\frac{b^{2}}{\\left(1 - b^{2}\\right) \\tan{\\left(\\theta \\right)}}$"
      ],
      "text/plain": [
       "Eq(tan(phi), b**2/((1 - b**2)*tan(theta)))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(phi),b**2/(tan(theta)*(1-b**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cb782319",
   "metadata": {},
   "outputs": [],
   "source": [
    "t_square=(b**2*tan(phi)/(tan(phi)+tan(theta))*(1+(1/tan(phi))**2)).subs(tan(phi),b**2/(tan(theta)*(1-b**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "82c3e2aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle t^{2} = \\frac{b^{4} + \\left(b^{2} - 1\\right)^{2} \\tan^{2}{\\left(\\theta \\right)}}{b^{2} + \\left(1 - b^{2}\\right) \\tan^{2}{\\left(\\theta \\right)}}$"
      ],
      "text/plain": [
       "Eq(t**2, (b**4 + (b**2 - 1)**2*tan(theta)**2)/(b**2 + (1 - b**2)*tan(theta)**2))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(t**2,simplify(t_square))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "802f7ecc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan^{2}{\\left(\\theta \\right)} = \\frac{b^{2} \\left(a^{2} - 1\\right)}{a^{2} \\cdot \\left(1 - b^{2}\\right)}$"
      ],
      "text/plain": [
       "Eq(tan(theta)**2, b**2*(a**2 - 1)/(a**2*(1 - b**2)))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(theta)**2,b**2*(a**2-1)/(a**2*(1-b**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b31c2b81",
   "metadata": {},
   "outputs": [],
   "source": [
    "t_ab=simplify(t_square).subs(tan(theta)**2,b**2*(a**2-1)/(a**2*(1-b**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "053f5be5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle t^{2} = \\frac{a^{2} + b^{2} - 1}{2 a^{2} - 1}$"
      ],
      "text/plain": [
       "Eq(t**2, (a**2 + b**2 - 1)/(2*a**2 - 1))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(t**2,simplify(t_ab))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ceb10b7a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan^{2}{\\left(\\phi \\right)} = \\frac{a^{2} b^{2}}{\\left(1 - b^{2}\\right) \\left(a^{2} - 1\\right)}$"
      ],
      "text/plain": [
       "Eq(tan(phi)**2, a**2*b**2/((1 - b**2)*(a**2 - 1)))"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(phi)**2,(a*b)**2/((1-b**2)*(a**2-1)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e16eaf27",
   "metadata": {},
   "source": [
    "其中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f1b4b8df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan^{2}{\\left(\\theta \\right)} = \\frac{b^{2} \\left(a^{2} - 1\\right)}{a^{2} \\cdot \\left(1 - b^{2}\\right)}$"
      ],
      "text/plain": [
       "Eq(tan(theta)**2, b**2*(a**2 - 1)/(a**2*(1 - b**2)))"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(theta)**2,b**2*(a**2-1)/(a**2*(1-b**2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "750d5570",
   "metadata": {},
   "source": [
    "于是，我们可以得到基本的变换公式：\n",
    "\n",
    "设斜二测画法的两个参数为$\\phi,t$,旋转和伸缩变换的参数为$\\theta,a,b$。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "783a13a3",
   "metadata": {},
   "source": [
    "我们可以得到如下变换等价公式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ca9c85a2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle t^{2} = \\frac{a^{2} + b^{2} - 1}{2 a^{2} - 1}$"
      ],
      "text/plain": [
       "Eq(t**2, (a**2 + b**2 - 1)/(2*a**2 - 1))"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(t**2,simplify(t_ab))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d5056d11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\tan^{2}{\\left(\\phi \\right)} = \\frac{a^{2} b^{2}}{\\left(1 - b^{2}\\right) \\left(a^{2} - 1\\right)}$"
      ],
      "text/plain": [
       "Eq(tan(phi)**2, a**2*b**2/((1 - b**2)*(a**2 - 1)))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(tan(phi)**2,(a*b)**2/((1-b**2)*(a**2-1)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01ba995a",
   "metadata": {},
   "source": [
    "其中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bb4906a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fb560db6",
   "metadata": {},
   "source": [
    "### 对于标准的斜二测画法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c40000b",
   "metadata": {},
   "source": [
    "$\\phi=\\frac{\\pi}{4},t=\\frac{1}{2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "dc444fc7",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'expr2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_123394/1402977314.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0manswer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnonlinsolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mEq\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mphi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mexpr2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mphi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mpi\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mEq\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtheta\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mtan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtheta\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mRational\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtheta\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'expr2' is not defined"
     ]
    }
   ],
   "source": [
    "answer=nonlinsolve([Eq(tan(phi),-1/expr2).subs(phi,pi/4),Eq(t**2,(a**2+(tan(theta)/a)**2)/(1+tan(theta)**2)).subs(t,Rational(1,2))],[a,tan(theta)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6af0288",
   "metadata": {},
   "outputs": [],
   "source": [
    "answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73da62e5",
   "metadata": {},
   "source": [
    "即$a,\\tan(\\theta)$为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1eab53e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "answer.args[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebde6448",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7e8fa65",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4640cda",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "872c2904",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "548c8a16",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bc1a47f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b5d4635",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
